package com.scott.producer;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 生产者 Producer
 */
public class Producer implements Runnable {
	
	//生产这是否在工作 在运行 在做事
	private boolean isRunning = true;
	
	//基于线程安全的队列  树洞
	@SuppressWarnings("unused")
	private BlockingQueue<PCData> queue = null;
	
	private static AtomicInteger counter = new AtomicInteger();

	private static final int SLEEP_TIME=1000;

	public Producer(BlockingQueue<PCData> queue) {
		super();
		this.queue = queue;
	}

	@Override
	public void run() {
		@SuppressWarnings("unused")
		PCData data = null;
		Random random = new Random();
		System.out.println("启动生产者["+Thread.currentThread().getId()+"]...");
		
		try {
			while(isRunning){
				//因为不知道消费者什么时候过来。先随机休息会
				Thread.sleep(random.nextInt(SLEEP_TIME));
				//随意给它一个数据
				data = new PCData(counter.incrementAndGet());
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		
	}
	
	
}
